<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-list-swipe.css" />
    <style>
        body {}
    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            指示配送备货
        </div>
    </header>
    <div class="aui-content">
        <div class="aui-form">

            <div class="aui-input-row" style="font-size: 18px;">
                <span class="aui-input-addon"
                    style="width: 20%;text-align: left;font-weight: 600;font-size: 0.8rem;">箱号扫描:</span>
                <input type="text" class='aui-input' placeholder="" style="width: 80%;" id="scanCode" />
                <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="doScan()"></i>
            </div>

        </div>
        <div style="background-color:white; height: 10rem;width: 100%;">
            <p id="planQty" style="font-size:0.9rem;color: #1abc9c;"></p>
            <div style="height:9rem;overflow-y:auto;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <!-- <td algin='center' style="width:50px;">序号</td> -->
                        <td algin='center' style="width:120px;;">物料号</td>
                        <td algin='center' style="width:120px;">配送数量</td>
                        <td algin='center' style="width:100px;">批次号</td>
                        <td algin='center' style="width:100px;">包装数</td>
                        <td algin='center' style="width:90px;">源库</td>
                        <td algin='center' style="width:100px;">源库位</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml" >
                </tbody>
            </table>
          </div>
        </div>

        <div style="background-color:white; height: 100%;width: 100%;padding-top: 1px;">
            <p id="countQty" style="font-size:0.9rem;color: #e51c23;padding-top: 4px;height: 1.5rem;"></p>
            <table class="table" style='width:100%;' id="tableS">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <!-- <td algin='center' style="width:50px;">序号</td> -->
                        <td algin='center' style="width:120px;;">物料号</td>
                        <td algin='center' style="width:120px;;">物料名称</td>
                        <td algin='center' style="width:100px;">箱号</td>
                        <td algin='center' style="width:100px;">批次号</td>
                        <td algin='center' style="width:100px;">数量</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtmlS">
                </tbody>
            </table>
        </div>
    </div>


    <!-- <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer> -->
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../script/aui-list-swipe.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var scanRule = ""; //条码解析规则
    apiready = function () {
        var paTaskCode = api.pageParam.paTaskCode;
        var mrlCode = api.pageParam.mrlCode;

        $("#scanCode").focus();
        idSelector = "scanCode";

        getPaTaskDetailInfo(paTaskCode, mrlCode);
        getPaDistributeMrlRels(paTaskCode, mrlCode);
        //查询条码解析规则
        initScanRule();
    };

    var swipe = new ListSwipe();
    var planQty=0;
    var countQty=0;

    function closeWin() {
        api.closeWin();
        api.execScript({
            name: 'paAndonDistribute',
            script: 'getDistributeMrlInfo("' + api.pageParam.paTaskCode + '");'
        });
    }

    // 扫描配送任务号
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150,//扫描窗口的上边距
            width: 250,//扫描窗口的宽
            height: 250,//扫描窗口的高
            useColor: false,//是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF',//渐变起始颜色
            endColor: '#7F08C8FF',//渐变结束颜色
            animTime: 2200,//扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //解析条码
            tmSplit(ret);
        });
    }
    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描回车事件
    $("#scanCode").on('keypress', function (e) { //#keyword为input文本框

        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            console.log("毁车事件");
            $("#scanCode").val("");
            tmSplit(sm);
        }
    });
    //扫描之后条码解析
    function tmSplit1(sm) {
        console.log("条码解析");
        var arr = sm.split(",");
        //$("#tm").val(sm);
        var mrlCode = arr[1];
        var qty = arr[2];
        var lotCode = arr[3];
        var boxnum = arr[9];
        if (api.pageParam.mrlCode == mrlCode) {
            //保存备货信息
            saveMrl(mrlCode, boxnum, lotCode, qty,sm);
        } else {
            api.toast({
                msg: "物料号不匹配！",
                duration: 3000
            });
        }
        $("#scanCode").focus();
        idSelector = "scanCode";
    }

    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }
    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            $("#mrlCode").val(arr[1]);
        }
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            $("#qty").val(arr[2]);
        }
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            $("#lotCode").val(arr[3]);
        }
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            $("#tmID").val(arr[4]);
        }
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            $("#boxnum").val(arr[9]);
        }

        var mrlCode = arr[1];
        var qty = arr[2];
        var lotCode = arr[3];
        var boxnum = arr[9];
        var tmId = arr[4];
        if (api.pageParam.mrlCode == mrlCode) {
            //保存备货信息
            saveMrl(mrlCode, boxnum, lotCode, qty,sm,tmId);
        } else {
            api.toast({
                msg: "物料号不匹配！",
                duration: 3000
            });
        }
    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    var mrlCode = map["mrlCode"];
                    var qty = map["qty"];
                    var lotCode = map["lotCode"];
                    var boxnum = map["boxnum"];
                    var tmId = map["tmID"];
                    if (api.pageParam.mrlCode == mrlCode) {
                        //保存备货信息
                        saveMrl(mrlCode, boxnum, lotCode, qty,sm,tmId);
                    } else {
                        api.toast({
                            msg: "物料号不匹配！",
                            duration: 3000
                        });
                    }
                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    // 获取-对应的 配送任务明细信息
    function getPaTaskDetailInfo(paTaskCode, mrlCode) {
        console.log("paTaskCode", paTaskCode);
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchPaTaskDetailInfo";

        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        var un = localStorage.getItem('un');
        var data = {
            values: {
                paTaskCode: paTaskCode,
                mrlCode: mrlCode,
                un: un
            }
        };
        console.log("un", un);
        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
					url: url,
					method: 'post',
					dataType: 'json',
					timeout: 300,
					returnAll: false,
					data: data
				}, function(ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
								var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = myobj.list;
                    planQty = myobj.planqty;
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {

                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;' onclick='goDetailPge();'>";
                        html += "<td name='mrlCode'>" + list[i].subMrlcode + "</td>";
                        html += "<td name='planqty'>" + list[i].planqty + "</td>";
                        html += "<td name='uda2n'>" + list[i].ext17 + "</td>";
                        html += "<td name='uda3c'>" + list[i].ext18 + "</td>";
                        html += "<td name='uda4c'>" + (list[i].ext19==null?"":list[i].ext19) + "</td>";
                        html += "<td name='uda4n'>" + (list[i].ext20==null?"":list[i].ext20) + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                    $api.html($api.byId("planQty"), "计划数量：" + planQty);

                } else if (myobj.code == "1") {
                    //失败
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }
    function getPaDistributeMrlRels(paTaskCode, mrlCode) {
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchPaDistributeMrlRels";

        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        var un = localStorage.getItem('un');
        var data = {
            values: {
                paTaskCode: paTaskCode,
                mrlCode: mrlCode,
                un: un
            }
        };

        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
					url: url,
					method: 'post',
					dataType: 'json',
					timeout: 300,
					returnAll: false,
					data: data
				}, function(ret, err) {
            if (ret) {
                api.hideProgress();
                console.log("retretretret"+ret);
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = myobj.list;
                    countQty = myobj.countQty;
                    var html = "";
                    console.log("listlist"+list);
                    //遍历
                    for (var i = 0; i < list.length; i++) {

                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;' onclick='delDetail(\"" + list[i].boxCode + "\")'>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td name='boxCode'>" + list[i].boxCode + "</td>";
                        html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                        html += "<td name='qty'>" + list[i].qty + "</td>";
                        html += "</tr>";
                    }


                    $api.html($api.byId("tbodyHtmlS"), html);
                    $api.html($api.byId("countQty"), "备货数量：" + countQty);
                    console.log("countQty"+Number(countQty));
                    console.log("planQty11"+planQty);
                    console.log(Number(countQty) > Number(planQty));
                    if (Number(countQty) > Number(planQty)) {
                        //若备货数量>=计划数量，则改变字体颜色为绿色
                        $api.html($api.byId("countQty"), "备货数量：" + countQty + " （已满足）");
                        document.getElementById("countQty").style.color = "#1abc9c";
                    } else {
                        $api.html($api.byId("countQty"), "备货数量：" + countQty + " （未满足）");
                    }

                } else if (myobj.code == "1") {
                    //失败
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    // 保存箱号信息
    function saveMrl(mrlCode, boxnum, lotCode, qty,tm,tmId) {

        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/savePaDistributeMrlRel";

        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        var un = localStorage.getItem('un');
        var data = {
            values: {
                paTaskCode: api.pageParam.paTaskCode,
                mrlCode: mrlCode,
                boxCode: boxnum,
                lotCode: lotCode,
                tm: tm,
                tmId: tmId,
                qty: qty,
                un: un
            }
        };
        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
          url: url,
          method: 'post',
          dataType: 'json',
          timeout: 300,
          returnAll: false,
          data: data
        }, function(ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var paDistributeMrlRel = myobj.paDistributeMrlRel;

                    var html = "";
                    html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;' onclick='delDetail(\"" + paDistributeMrlRel.boxCode + "\")'>";
                    html += "<td name='mrlCode'>" + paDistributeMrlRel.mrlCode + "</td>";
                    html += "<td name='mrlName'>" + paDistributeMrlRel.mrlName + "</td>";
                    html += "<td name='boxCode'>" + paDistributeMrlRel.boxCode + "</td>";
                    html += "<td name='lotCode'>" + paDistributeMrlRel.lotCode + "</td>";
                    html += "<td name='qty'>" + paDistributeMrlRel.qty + "</td>";
                    html += "</tr>";
                    $("#tbodyHtmlS").append(html);
                    countQty = Number(countQty) + Number(paDistributeMrlRel.qty);
                    console.log("countQty", countQty);
                    $api.html($api.byId("countQty"), "备货数量：" + countQty);
                    if (Number(countQty) >= Number(planQty)) {
                        console.log("改改嘎");
                        console.log(document.getElementById("countQty").style.color);
                        //若备货数量>=计划数量，则改变字体颜色为绿色
                        document.getElementById("countQty").style.color = "#1abc9c";
                    }
                } else if (myobj.code == "1") {
                    //失败
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }
    //删除备货箱
    function delDetail(boxCode) {
        api.confirm({
            title: '提示',
            msg: "确认撤销箱号 【" + boxCode + "】物料？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/delPaDistributeMrlRel";

                var params = new Object();
                params.url = url;
                params.method = 'post';
                params.dataType = 'json';
                var un = localStorage.getItem('un');
                var data = {
                    values: {
                        paTaskCode: api.pageParam.paTaskCode,
                        mrlCode: api.pageParam.mrlCode,
                        boxCode: boxCode,
                        un: un
                    }
                };
                api.showProgress({
                    title: '保存中...',
                });
                api.ajax({
                  url: url,
                  method: 'post',
                  dataType: 'json',
                  timeout: 300,
                  returnAll: false,
                  data: data
                }, function(ret, err) {
                    if (ret) {
                        api.hideProgress();
                        var data = JSON.stringify(ret);
                        var myobj = eval("(" + data + ")");
                        //返回成功
                        if (myobj.code == "0") {
                            getPaDistributeMrlRels( api.pageParam.paTaskCode, api.pageParam.mrlCode);

                        } else if (myobj.code == "1") {
                            //失败
                            api.toast({
                                msg: myobj.message,
                                duration: 3000
                            });
                        }
                    }
                });
            }
        });
    }


</script>

</html>
